<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="资产名称" prop="assetName">
        <el-input
          v-model="queryParams.assetName"
          placeholder="请输入资产名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="档案编号" prop="barcode">
        <el-input
          v-model="queryParams.barcode"
          placeholder="请输入档案编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="spec">
        <el-input
          v-model="queryParams.spec"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商名称" prop="supplierName">
        <el-select clearable style="width: 100%" filterable v-model="queryParams.supplierName" placeholder="供应商" >
          <el-option
            v-for="dict in supplyOptions"
            :key="dict.supplierCode"
            :label="dict.supplierName"
            :value="dict.supplierName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="shStatus">
        <el-select v-model="queryParams.shStatus" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.zcrk_sh_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="制造商名称" prop="manufactureName">-->
<!--        <el-input-->
<!--          v-model="queryParams.manufactureName"-->
<!--          placeholder="请输入制造商名称"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sales:AssetDb:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sales:AssetDb:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sales:AssetDb:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sales:AssetDb:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="info"
            plain
            icon="el-icon-upload2"
            size="mini"
            @click="handleImport"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-s-opportunity"
          size="mini"
          :disabled="multiple"
          @click="handleSh"
        >审核</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-s-opportunity"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleQx"-->
<!--        >取消审核</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="AssetDbList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键ID" align="center" prop="id" />-->
<!--      <el-table-column label="组织代码" align="center" prop="orgCode" />-->
<!--      <el-table-column label="组织名称" align="center" prop="orgName" />-->
<!--      <el-table-column label="调出部门" align="center" prop="deptOut" />-->
      <el-table-column width="120" label="调入部门" align="center" prop="deptIn" />
<!--      <el-table-column width="150" label="资产代码" align="center" prop="assetNumber" />-->
      <el-table-column label="分类名称" align="center" prop="assetType" />
      <el-table-column label="子类名称" align="center" prop="assetClass" />
      <el-table-column label="资产编码" align="center" prop="assetCode" />
      <el-table-column label="资产名称" align="center" prop="assetName" />
      <el-table-column width="150" label="档案编号" align="center" prop="barcode" />
<!--      <el-table-column width="150" label="物料识别码" align="center" prop="materialCode" />-->
      <el-table-column label="品牌" align="center" prop="brand" />
      <el-table-column label="规格型号" align="center" prop="spec" />
      <el-table-column label="购置时间" align="center" prop="buyTime" width="180"/>
      <el-table-column label="使用寿命" align="center" prop="useLife" />
      <el-table-column width="150" label="供应商编码" align="center" prop="supplierCode" />
      <el-table-column width="150" label="供应商名称" align="center" prop="supplierName" />
<!--      <el-table-column label="制造商编码" align="center" prop="manufactureCode" />-->
<!--      <el-table-column label="制造商名称" align="center" prop="manufactureName" />-->
<!--      <el-table-column label="资产图片" align="center" prop="assetImg" width="100">-->
<!--        <template slot-scope="scope">-->
<!--          <image-preview :src="scope.row.assetImg" :width="50" :height="50"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="资产保养周期" align="center" prop="assetMaintenance" />-->
<!--      <el-table-column label="资产维护周期" align="center" prop="assetPhold" />-->
<!--      <el-table-column label="资产程序代码" align="center" prop="assetProgramCode" />-->
<!--      <el-table-column label="资产程序包" align="center" prop="assetProgramPackage" />-->
<!--      <el-table-column label="计划数量" align="center" prop="plannNum" />-->
      <el-table-column label="调拨数量" align="center" prop="dbNum" />
<!--      <el-table-column label="单价" align="center" prop="unitPrice" />-->
<!--      <el-table-column label="金额" align="center" prop="amount" />-->
      <el-table-column label="审核状态" align="center" prop="shStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zcrk_sh_state" :value="scope.row.shStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="shName" />
      <el-table-column width="120" label="审核时间" align="center" prop="shDate" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sales:AssetDb:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sales:AssetDb:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资产调拨对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row :gutter="24">
<!--          <el-col :span="12">-->
<!--            <el-form-item label="调出部门" prop="deptOut">-->
<!--              <el-input v-model="form.deptOut" placeholder="请输入调出部门" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->

          <el-col :span="12">
            <el-form-item label="资产代码" prop="assetNumber">
              <el-input @click.native="zcShowOpen" disabled suffix-icon="el-icon-date" v-model="form.assetNumber" placeholder="请输入资产代码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="调入部门" prop="deptIn">
              <treeselect @select="handleSelect"  v-model="form.deptIn" :clearable="true" :options="deptOptions" :show-count="true" placeholder="请选择调入部门" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="资产编码" prop="assetCode">
              <el-input disabled v-model="form.assetCode" placeholder="请输入资产编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资产大类" prop="assetClass">
              <el-input disabled v-model="form.assetClass" placeholder="请输入资产大类" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="资产名称" prop="assetName">
              <el-input disabled v-model="form.assetName" placeholder="请输入资产名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="条码号" prop="barcode">
              <el-input disabled v-model="form.barcode" placeholder="请输入条码号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="物料识别码" prop="materialCode">
              <el-input disabled v-model="form.materialCode" placeholder="请输入物料识别码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="品牌" prop="brand">
              <el-input disabled v-model="form.brand" placeholder="请输入品牌" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="规格型号" prop="spec">
              <el-input disabled v-model="form.spec" placeholder="请输入规格型号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="购置时间" prop="buyTime">
              <el-date-picker clearable
                v-model="form.buyTime"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择购置时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="使用寿命" prop="useLife">
              <el-input disabled v-model="form.useLife" placeholder="请输入使用寿命" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商编码" prop="supplierCode">
              <el-input disabled v-model="form.supplierCode" placeholder="请输入供应商编码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="供应商名称" prop="supplierName">
              <el-input disabled v-model="form.supplierName" placeholder="请输入供应商名称" />
            </el-form-item>
          </el-col>
<!--          <el-col :span="12">-->
<!--            <el-form-item label="制造商编码" prop="manufactureCode">-->
<!--              <el-input v-model="form.manufactureCode" placeholder="请输入制造商编码" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="制造商名称" prop="manufactureName">-->
<!--              <el-input v-model="form.manufactureName" placeholder="请输入制造商名称" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="24">-->
<!--            <el-form-item label="资产图片" prop="assetImg">-->
<!--              <image-upload v-model="form.assetImg"/>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="资产保养周期" prop="assetMaintenance">-->
<!--              <el-input v-model="form.assetMaintenance" placeholder="请输入资产保养周期" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="资产维护周期" prop="assetPhold">-->
<!--              <el-input v-model="form.assetPhold" placeholder="请输入资产维护周期" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="资产程序代码" prop="assetProgramCode">-->
<!--              <el-input v-model="form.assetProgramCode" placeholder="请输入资产程序代码" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="资产程序包" prop="assetProgramPackage">-->
<!--              <el-input v-model="form.assetProgramPackage" placeholder="请输入资产程序包" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="计划数量" prop="plannNum">-->
<!--              <el-input v-model="form.plannNum" placeholder="请输入计划数量" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->

          <el-col :span="12">
            <el-form-item label="调拨数量" prop="dbNum">
              <el-input-number style="width: 80%" size="small" v-model="form.dbNum" placeholder="请输入调拨数量" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
<!--          <el-col :span="12">-->
<!--            <el-form-item label="单价" prop="unitPrice">-->
<!--              <el-input-number style="width: 80%" size="small" v-model="form.unitPrice" placeholder="请输入单价" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
<!--          <el-col :span="12">-->
<!--            <el-form-item label="金额" prop="amount">-->
<!--              <el-input disabled v-model="form.amount" placeholder="请输入金额" />-->
<!--            </el-form-item>-->
          </el-col>
        </el-row>
<!--        <el-row :gutter="24">-->
<!--          <el-col :span="24">-->
<!--            <el-form-item label="备注" prop="remark">-->
<!--              <el-input v-model="form.remark" placeholder="请输入备注" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--        </el-row>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
          ref="upload"
          :limit="1"
          accept=".xlsx, .xls"
          :headers="upload.headers"
          :action="upload.url + '?updateSupport=' + upload.updateSupport"
          :disabled="upload.isUploading"
          :on-progress="handleFileUploadProgress"
          :on-success="handleFileSuccess"
          :auto-upload="false"
          drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!--  <div class="el-upload__tip" slot="tip">
              <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
            </div>-->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="资产档案" :visible.sync="zcShow" width="1200px" append-to-body>
      <el-row>
        <el-col :span="24">
          <DbAdd :getForm="form" ref="dbAdd"></DbAdd>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitDb">确 定</el-button>
        <el-button @click="zcShow = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAssetDb, getAssetDb, delAssetDb, addAssetDb, updateAssetDb,dbsh,dbshqx } from "@/api/sales/AssetDb";
import {getToken} from "@/utils/auth";
import Treeselect from "@riophae/vue-treeselect";
import { getWarehouseInfoByDeptId } from "@/api/base/warehouseinfo";
import { treeselect } from "@/api/system/dept";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import DbAdd from "./dbAdd";
import RukuAdd from "@/views/sales/AssetRuku/rukuAdd2.vue";
import {cksh, ckshqx} from "@/api/sales/AssetChuku";
import {getLiSupplier} from "@/api/supplychain/supplier";
export default {
  name: "AssetDb",
  components: {RukuAdd, Treeselect,DbAdd},
  dicts: ['zcrk_sh_state'],
  data() {
    return {
      supplyOptions: [],
      formSh:{},
      zcShow:false,
      deptOptions:undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资产调拨表格数据
      AssetDbList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        assetType: null,
        assetName: null,
        barcode: null,
        spec: null,
        supplierName: null,
        manufactureName: null,
      },
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "导入`AssetDb`信息",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/sales/AssetDb/importData"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        assetNumber: [
          { required: true, message: "资产代码不能为空", trigger: "blur" }
        ],
        deptIn: [
          { required: true, message: "请选择调入部门", trigger: "change" }
        ],
        dbNum: [
          { required: true, message: "调拨数量不能为空", trigger: "blur" }
        ],
        // unitPrice: [
        //   { required: true, message: "单价不能为空", trigger: "blur" }
        // ],
      }
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
    getLiSupplier().then(response => {
      this.supplyOptions = response.data;
    });
  },
  methods: {
    //审核
    handleSh(){
      this.formSh.ids = this.ids
      dbsh(this.formSh).then(res=>{
        this.$message.success('审核成功')
        this.getList()
      })

    },
    //取消审核
    handleQx(){
      this.formSh.ids = this.ids
      dbshqx(this.formSh).then(res=>{
        this.$message.success('取消审核成功')
        this.getList()
      })

    },
    submitDb(){
      let selectedArrData = this.$refs.dbAdd.seles

      this.form = selectedArrData
      this.form.dbNum = 1
      this.form.id = ''
      this.zcShow = false
    },
    zcShowOpen(){
      // this.$refs.dbAdd.getList();
      this.zcShow = true
      // this.$refs.dbAdd.currentRow = {}
    },
    handleSelect(e){
      this.form.deptName = e.label
      this.form.deptIn = e.label
      this.form.deptIdIn = e.id

    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    /** 查询资产调拨列表 */
    getList() {
      this.loading = true;
      listAssetDb(this.queryParams).then(response => {
        this.AssetDbList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orgCode: null,
        orgName: null,
        deptOut: null,
        deptIn: null,
        assetNumber: null,
        assetCode: null,
        assetType: null,
        assetClass: null,
        assetName: null,
        barcode: null,
        materialCode: null,
        brand: null,
        spec: null,
        buyTime: null,
        useLife: null,
        supplierCode: null,
        supplierName: null,
        manufactureCode: null,
        manufactureName: null,
        assetImg: null,
        assetMaintenance: null,
        assetPhold: null,
        assetProgramCode: null,
        assetProgramPackage: null,
        plannNum: null,
        dbNum: null,
        unitPrice: null,
        amount: null,
        remark: null,
        createTime: null,
        createBy: null,
        free1: null,
        free2: null,
        free3: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {

      // this.reset();
      // this.$refs.dbAdd.getList()
      this.open = true;
      this.title = "添加资产调拨";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAssetDb(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改资产调拨";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.deptIn = this.form.deptName
      // this.form.amount = ((this.form.dbNum-0) * (this.form.unitPrice-0)).toFixed(2)
      this.form.id = null
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAssetDb(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAssetDb(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除数据？').then(function() {
        return delAssetDb(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sales/AssetDb/export', {
        ...this.queryParams
      }, `AssetDb_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "资产调拨导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('sales/AssetDb/importTemplate', {
      }, `资产调拨模板.xlsx`)
    },
    /** 文件上传中处理 */
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    /** 文件上传成功处理 */
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    /** 提交上传文件 */
    submitFileForm() {
      this.$refs.upload.submit();
    },
  }
};
</script>
